/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $:
 * @version $Revision: 364 $:
 */
package dk.hewison.client.mvc;

import dk.hewison.client.AppEvent;
import dk.hewison.client.Log;

import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Register {

    private Map<AppEvent.Event, Set<Controller>> events = new EnumMap<AppEvent.Event, Set<Controller>>(AppEvent.Event.class);

    public void registerEvent(AppEvent.Event eventId, Controller controller) {
        Set<Controller> controllerList = events.get(eventId);
        if (controllerList == null) {
            controllerList = new HashSet<Controller>();
            events.put(eventId, controllerList);
        }
        if (!controllerList.contains(controller)) {
            controllerList.add(controller);
        }
    }

    public void eventOccurred(AppEvent event) {
        Set<Controller> controllers = events.get(event.getEvent());
        if (controllers == null) {
            Log.debug("No controllers for Event " + event.getEvent());
            return;
        }
        for (Controller controller : controllers) {
            controller.eventOccurred(event);
        }
    }

    private static Register ourInstance = new Register();

    public static Register getInstance() {
        return ourInstance;
    }

    private Register() {
    }
}
